package com.gmzg.gmzgoffice.works;


import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmzg.gmzgoffice.GMZGApplication;
import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Approval;
import com.gmzg.gmzgoffice.entity.Person;
import com.gmzg.gmzgoffice.works.adapter.RecyclerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class ApproveFragment extends Fragment implements RecyclerAdapter.OnChildClickListener, SwipeRefreshLayout.OnRefreshListener, ApproveActivity.SetTabPositionListener {

    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;
    private int mPosition = 0;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private List<Approval> mList;
    private RecyclerAdapter mAdapter;

    public ApproveFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mList = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_approve, container, false);
        ButterKnife.bind(this, view);
        //获取ApproveActivity中穿过来的值
        Bundle bundle = getArguments();
        int i = bundle.getInt(Constant.CATEGORY, -1);
        if (i != -1){
            switch (i){
                case 0:
                    getListData();
                    Log.d("gjj", "onCreateView0: "+i);
                    break;
                case 1:
                    Log.d("gjj", "onCreateView1: "+i);

                    break;
                case 2:
                    Log.d("gjj", "onCreateView2: "+i);

                    break;
            }
        }

        // TODO: 2016/11/9 获取来自数据库的文件
        //测试数据
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 50; i++) {
//            list.add(String.format(Locale.CHINA,"标题 %02d",i));
//        }
        String userName = ((GMZGApplication) getActivity().getApplication()).getUserName();
        mAdapter = new RecyclerAdapter(mList, getActivity(),userName);
        //条目的点击事件
        mAdapter.setOnChildClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        //添加刷新监听事件
        mRefresh.setOnRefreshListener(this);
        ((ApproveActivity) getActivity()).setListener(this);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
//        mRefresh.setRefreshing(true);
    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position, Approval approval) {
        Intent intent = new Intent(getActivity(), LeaveApplicationActivity.class);
        intent.putExtra(Constant.APPROVAL,approval);
        intent.putExtra(Constant.APPROVAL_ID,approval.getObjectId());
        getActivity().startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(int position) {
        mPosition = position;
        Log.d("gao", "onEvent: "+mPosition);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRefresh() {
        getListData();

    }

    private void getListData() {
        mRefresh.setRefreshing(true);
        switch (mPosition){
            case 0:
                String tab0 = "who_approval";
                getData(tab0,mPosition);
                break;
            case 1:
                String tab1 = "who_approval";
                getData(tab1,mPosition);
                break;
            case 2:
                //获取我发起的数据
                String tab02 = "user_name";
                getData(tab02,mPosition);
                break;
        }
    }

    private void getData(String tab02, final int position) {
        BmobQuery<Approval> query = new BmobQuery<Approval>();
        query.addWhereEqualTo(tab02, ((GMZGApplication) getActivity().getApplication()).getUserName());
        query.findObjects(new FindListener<Approval>() {
            @Override
            public void done(List<Approval> list, BmobException e) {
                if (e == null) {
                    if (list != null) {
                        switch (position){
                            case 0:
                                for (Approval approval : list) {
                                    if (approval.getApproval_Status())
                                        list.remove(approval);
                                }
                                break;
                            case 1:
                                for (Approval approval : list) {
                                    if (!approval.getApproval_Status())
                                        list.remove(approval);
                                }
                                break;
                            case 2:
                                break;
                        }
                        mAdapter.addList(list);
                    }
                }else {
//                    Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
                }
                mRefresh.setRefreshing(false);
            }
        });
    }

    @Override
    public void setTabPosition(int position) {
        mPosition = position;
        Log.d("haha", "setTabPosition: "+position);
    }
}
